package rabbit.flt.plugins.reactor.http.plugin;

import rabbit.flt.common.trace.TraceData;
import rabbit.flt.plugins.common.MethodCallback;
import rabbit.flt.plugins.common.plugin.SupportPlugin;
import rabbit.flt.plugins.reactor.http.plugin.impl.ResponseConnectionPlugin;
import rabbit.flt.plugins.reactor.http.plugin.impl.ResponsePlugin;
import rabbit.flt.plugins.reactor.http.plugin.impl.ResponseSinglePlugin;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class HttpClientFinalizerPlugin extends SupportPlugin {

    /**
     * trace data在context中的名字
     */
    public static final String CONTEXT_KEY_NAME = "reactor_trace_data_key_name";

    private Map<String, SupportPlugin> plugins = new HashMap<>();

    public HttpClientFinalizerPlugin() {
        plugins.put("responseSingle", new ResponseSinglePlugin());
        plugins.put("response", new ResponsePlugin());
        plugins.put("responseConnection", new ResponseConnectionPlugin());
    }

    @Override
    public Object[] before(Object objectEnhanced, Method method, Object[] args) {
        return plugins.get(method.getName()).before(objectEnhanced, method, args);
    }


    /**
     * 调用  responseSingle/response 方法
     *
     * @param objectEnhanced
     * @param method
     * @param callback
     * @param args
     * @return
     */
    @Override
    public Object invokeMethod(Object objectEnhanced, Method method, MethodCallback callback, Object[] args) {
        return plugins.get(method.getName()).invokeMethod(objectEnhanced, method, callback, args);
    }

    @Override
    protected void handleTraceData(TraceData traceData) {
        // do nothing
    }
}
